﻿module Enferno.Public.Web.Components.Models {

    export class CompanyModel {
        public deliveryAddresses: Array<AddressModel>;
        public invoiceAddress: AddressModel;
        
        public companyInformation: CompanyInformationModel;
        public organisationNumber: string;
        
        public useInvoiceAddressAsDeliveryAddress: boolean;

        constructor();
        constructor(serverObject: any);
        constructor(serverObject?: any) {
            if (serverObject != null) {
                this.createFromServerObject(serverObject);
            }
        }

        private createFromServerObject(serverObject: any) {
            this.deliveryAddresses = serverObject.deliveryAddresses != null ? Enumerable.from(serverObject.deliveryAddresses).select(deliveryAddress => new AddressModel(deliveryAddress)).toArray() : [];
            this.invoiceAddress = serverObject.invoiceAddress != null ? new AddressModel(serverObject.invoiceAddress) : null;
            this.organisationNumber = serverObject.organisationNumber;
            this.useInvoiceAddressAsDeliveryAddress = serverObject.useInvoiceAddressAsDeliveryAddress;
        }
    }

    export class CompanyInformationModel {

        constructor(serverObject?: any) {
            if (serverObject != null) {
                this.name = serverObject.name;
                this.phone = serverObject.phone;
            }
        }

        public serialize(): any {
            var obj: any = {};
            obj.name = this.name;
            obj.phone = this.phone;

            return obj;
        }

        public isEmpty(): boolean {
            return this.name == null && this.phone == null;
        }

        public name: string;
        public phone: string;
    }
} 